package atguigu.spider;

import java.io.File;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Path;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;

/**
 * 抓取博客园的网页
 * @author ex_yejing1
 *
 */
public class SpiderDemo1 {

	public static void main(String[] args) {
		
		String urlString = "https://www.cnblogs.com/shelley2022/p/16372463.html";
//		String urlString = "https://www.cnblogs.com/chenyishi/archive/2022/06/13/16371199.html";
		String html = HttpUtil.get(urlString, Charset.forName("UTF-8"));
//		System.out.println(html);
		
		Document doc = Jsoup.parseBodyFragment(html);
		Element body = doc.body();
		
		
		
//		Elements allElements = body.getAllElements();
//		for (Element element : allElements) {
//			String tagName = element.tagName();
//			System.out.println(tagName);
//			System.out.println("===========");
//			String html2 = element.html();
//			System.out.println(html2);
//		}
		
		// 获取指定属性的标签
		// div id="mainContent"
		
		Elements elements = body.getElementsByAttributeValue("id", "cnblogs_post_body");
//		String html2 = elements.html();
//		System.out.println(html2);
		
//		System.out.println(elements.size());
		
		for (Element element : elements) {
//			showElementTagNameAndHtml(element);
//			Element firstElementSibling = element.firstElementSibling();
			Elements allElements = element.getAllElements();
			System.out.println(allElements.size());
			for (Element element2 : allElements) {
				System.out.println(element2.tagName());
			}
			
//			Element element2 = allElements.get(1);
//			showElementTagNameAndHtml(element2);
			
//			for (Element element2 : allElements) {
//				showElementTagNameAndHtml(element2);
//			}
		}
		

		
//		Elements h3elements = body.getElementsByTag("h3");
//		String html2 = h3elements.html();
//		System.out.println(html2);
//		
		// 根据标签名，读取到所有匹配的标签，然后获取所有标签的内容
//		Elements pelements = body.getElementsByTag("p");
//		String html3 = pelements.html();
//		System.out.println(html3);
		
		
		// 下载图片到本地文件
//		String imageUrl = "https://img2022.cnblogs.com/blog/2902536/202206/2902536-20220613215125741-1679169704.png";
//		File destFile = new File("D:/a.png");
//		HttpUtil.downloadFile(imageUrl, destFile);
		
		
		
		
//		Elements elementsByTag = body.getElementsByTag("h4");
//		String html2 = elementsByTag.html();
//		System.out.println(html2);
		
//		Elements elements = body.getElementsByTag("code");
//		String html3 = elements.html();
//		System.out.println(html3);
	}

	/**
	 * 显示element的tagName和html内容
	 * @param firstElementSibling
	 */
	private static void showElementTagNameAndHtml(Element element) {

		String tagName = element.tagName();
		String html = element.html();
		Attributes attributes = element.attributes();
		
		System.out.println("===tagname===");
		System.out.println(tagName);
		System.out.println("===html===");
		System.out.println(html);
		System.out.println("===attributes===");
		System.out.println(attributes);
		
	}
	
	
	
	
}
